﻿namespace TomatoBreak.Infrastructure
{
    using System;

    using DataAccess;
    using DomainObjects;
    using ExtensionMethods;

    public class HarvestSeedData : IRegistrar
    {
        private readonly IRepository<User> userRepository;
        private readonly IRepository<Task> taskRepository;

        public HarvestSeedData(IRepository<User> userRepository, IRepository<Task> taskRepository)
        {
            if (userRepository == null)
            {
                throw new ArgumentNullException("userRepository");
            }

            if (taskRepository == null)
            {
                throw new ArgumentNullException("taskRepository");
            }

            this.userRepository = userRepository;
            this.taskRepository = taskRepository;
        }

        public void Register()
        {
            Harvest();
        }

        private void Harvest()
        {
            const string UserName = "KaziManzurRashid";

            var user = userRepository.FindOne(u => u.UserName == UserName);

            if (user == null)
            {
                user = new User(UserName, "pa$$w0rd", "kazimanzurrashid@gmail.com");
                userRepository.Add(user);
            }

            if (taskRepository.Exists(t => t.User.Id == user.Id))
            {
                return;
            }

            for (int i = 1; i <= 3; i++)
            {
                var task = user.AddTask("Task {0}".Interpolate(i), Clock.UtcNow());

                taskRepository.Add(task);
            }
        }
    }
}